package edu.gatech.spacetraderteam3.view;

import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;

/**
 * Represents a text field in the GUI
 * @author Alexander Ikonomidis
 * @version 1.0
 */
public class SGGTextField extends SGGComponent {
    
	/**
	 * The text in this text field
	 */
	private String text;
	
	/**
	 * The max length of the textfield
	 */
    private int maxLength = 100; // TODO: make this... better. Ask me for details. -Alex
    
    /**
     *Boolean values for different attributes of the textfield
     */
    private boolean edited = false, acceptNumbers = true, acceptLetters = true,
            showBox = true;
    
    /**
     * The font of the textfield
     */
    private FontMetrics font;

    /**
     * Constructor that takes a position and initial text
     * @param originX X position
     * @param originY Y position
     * @param initText Initial text
     */
    public SGGTextField(int originX, int originY, String initText) {
        super(100, 10);
        setOrigin(originX, originY);
        text = initText;
    }

    /**
     * Constructor that takes initial text
     * @param initText Initial text
     */
    public SGGTextField(String initText) {
        this(0, 0, initText);
    }

    /**
     * Paints the TextFeild
     * @param g Graphics component to paint on
     */
    public void doPaint(Graphics2D g) {
        g.setColor(color);
        font = g.getFontMetrics();
        final int height = font.getHeight();
        // int width = font.stringWidth(text + "|");

        setDimensions(maxLength, height);
        g.drawString(text + "|", getX(), getY() + height - 2);
        if (showBox) {
            g.drawRect(getX(), getY(), getWidth(), getHeight());
        }
    }

    /**
     * Sets whether to accept letters
     * @param acceptLetters Whether to accept letters
     */
    public void setAcceptLetters(boolean acceptLetters) {
        this.acceptLetters = acceptLetters;
    }

    /**
     * Sets whether to accept numbers 
     * @param acceptNumbers Whether to accept numbers
     */
    public void setAcceptNumbers(boolean acceptNumbers) {
        this.acceptNumbers = acceptNumbers;
    }

    /**
     * Gets the current text of the text field
     * @return The text of the text field
     */
    public String getText() {
        return text;
    }

    /**
     * Sets the text of the text field
     * @param text Text to set
     */
    public void setText(String text) {
        this.text = text;
    }

    /**
     * Sets whether or not to show the background box
     * @param showBox Whether or not to show the background box
     */
    public void showBox(boolean showBox) {
        this.showBox = showBox;
    }

    /**
     * When a key is pressed, Checks to see if the key is valid (see accepted letters and numbers) and changes the Text Field if applicable
     * @param k The key that was pressed
     */
    protected void doKey(KeyEvent k) {
        if (!edited) {
            text = "";
        }
        edited = true;
        if (k.getKeyCode() == KeyEvent.VK_BACK_SPACE && text.length() > 0) {
            text = text.substring(0, text.length() - 1);
        } else if (font.stringWidth(text) < maxLength) {
            if (acceptNumbers && (48 <= k.getKeyChar() && k.getKeyChar() <= 57)) {
                text += k.getKeyChar();
            } else if (acceptLetters
                    && (k.getKeyCode() == 32
                            || (65 <= k.getKeyChar() && k.getKeyChar() <= 90) || (97 <= k
                            .getKeyChar() && k.getKeyChar() <= 122))) {
                text += k.getKeyChar();
            }
        }
        repaint();
    }

    /**
     * Setter for the maximum length 
     * @param maxLength Maximum length
     */
    public void setMaximumLength(int maxLength) {
        this.maxLength = maxLength;
    }

    /**
     * @return the currently entered text
     */
    public String toString() {
        return text;
    }
}
